package com.et114.modules.org.utility.tree;

import java.util.ArrayList;
import java.util.List;

import com.et114.core.common.AppContext;
import com.et114.core.dao.vo.BaseVO;
import com.et114.core.exception.CheckException;
import com.et114.modules.org.services.GroupManager;
import com.et114.modules.org.vo.GroupVo;
import com.et114.modules.org.vo.TitleMsgVo;
import com.et114.modules.org.vo.UserEmplyVo;

public class GroupTree {
    private static final String REL_TYPE_TE = "TE";
    private static final String REL_TYPE_GGT = "GGT";
//    private static final String REL_TYPE_GT = "GT";
//    private static final String REL_TYPE_GG = "GG";

    private static final String GROUP_ACTION_METHOD = "groupAction.go?method=";
    private static final String SRC_ACTION = GROUP_ACTION_METHOD + "loadPartyTree";
    private static final String GROUP_NODE_TGT_ACTION = "toGroupTabs.go";
    private static final String TITLE_NODE_TGT_ACTION = GROUP_ACTION_METHOD + "editTitle";
    private static final String EMP_NODE_TGT_ACTION = GROUP_ACTION_METHOD + "editEmp";
    private static final String TARGET = "frmRight";


    String grpNodeTgtAction;
    BaseVO root;
    int nodeMask = 1;
    List children = new ArrayList();
    List descendants = new ArrayList();
    GroupManager groupManager = (GroupManager) AppContext.getBean("groupManager");

    public void setGroupManager(GroupManager groupManager) throws CheckException {
        this.groupManager = groupManager;
    }

    public GroupTree(BaseVO baseVO) throws CheckException {
        this.root = baseVO;
        this.grpNodeTgtAction = GROUP_NODE_TGT_ACTION;
    }

    public GroupTree(BaseVO baseVO, String grpNodeTgtAction) throws CheckException {
        this.root = baseVO;
        this.grpNodeTgtAction = grpNodeTgtAction;
    }

    public String buildTree(BaseVO root, boolean hasRoot) throws CheckException {
        return HtcTree.buildTree(getNode(root), getChildNodes(), hasRoot);
    }

    public AbstractNode getRoot() throws CheckException {
        return getNode(root);
    }

    public List getChildren() throws CheckException {
        return ((GroupManager) groupManager).queryChildren(((BaseVO) root));
    }

    public List getChildNodes() throws CheckException {
        List list = getChildren();
        List nodeList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
        	BaseVO baseVO = (BaseVO) list.get(i);
        	switch (nodeMask) {
			case 1:
        		if (baseVO instanceof GroupVo) {
        			nodeList.add(getNode((BaseVO) list.get(i)));
        		}
				break;
			case 2:
        		if ((BaseVO) list.get(i) instanceof GroupVo) {
        			nodeList.add(getNode((BaseVO) list.get(i)));
        		}
				break;
			default:
				break;
			}
		}

        return nodeList;
    }

    public HtcTreeNode getNode(BaseVO baseVO) throws CheckException {
        HtcTreeNode node = new HtcTreeNode();
        // node.setChecked("checked");
        node.setTarget(TARGET);
        if (baseVO instanceof GroupVo) {
            GroupVo groupVo = (GroupVo) baseVO;
            node.setId(groupVo.getGrpType() + groupVo.getGrpId()); // TODO
            node.setName(groupVo.getChnName());
            node.setLeaf(isLeaf(baseVO));
            node.setSrcAction(SRC_ACTION + "&grpId=" + groupVo.getGrpId() + "&relType=" + REL_TYPE_GGT);
            node.setTgtAction(GROUP_NODE_TGT_ACTION + "?grpId=" + groupVo.getGrpId() + "&relType=" + REL_TYPE_GGT);
        }
        else if (baseVO instanceof TitleMsgVo) {
            TitleMsgVo titleMsgVo = (TitleMsgVo) baseVO;
            node.setId(String.valueOf(titleMsgVo.getTitleId()));
            node.setName(titleMsgVo.getTitleName());
            node.setLeaf(groupManager.queryEmpsOfTitle(((TitleMsgVo) baseVO).getTitleId()).size() == 0);
            node.setSrcAction(SRC_ACTION + "&titleId=" + titleMsgVo.getTitleId() + "&relType=" + REL_TYPE_TE);
            node.setTgtAction(TITLE_NODE_TGT_ACTION + "&titleId=" + titleMsgVo.getTitleId() + "&relType=" + REL_TYPE_TE);
        }
        else if (baseVO instanceof UserEmplyVo) {
            UserEmplyVo userEmplyVo = (UserEmplyVo) baseVO;
            node.setId(String.valueOf(userEmplyVo.getEmpId()));
            node.setName(userEmplyVo.getEmpName());
            node.setLeaf(true);
            node.setTgtAction(EMP_NODE_TGT_ACTION + "&empId=" + userEmplyVo.getEmpId());
        }
        else {
            throw new CheckException("Only tree organization structure!");
        }
        return node;
    }

    private boolean isLeaf(BaseVO baseVO) throws CheckException {
//        return groupManager.queryChildren(((GroupVo) baseVO)).size() == 0;
        return groupManager.queryChildGroups(((GroupVo) baseVO)).size() == 0;
    }


}
